<script language="javascript" type="text/javascript">
function captcha_refresh(){
	document.getElementById('captcha').src='{url}libs/captcha/captcha.php?'+Math.random();
    document.getElementById('comment_add').focus();
}
</script>
<?php

/**
 * @author Martin Pernica
 * @copyright 2008
 */
if (!defined('COM_COMMENT_BOOT')) {
    Text::p(Lang::getFront('com_comment', 'boot_fail'), 'msg error');
} else {

    Registry::get('page')->header('script', '{url}components/com_comment/bbeditor/ed.js');
    Registry::get('page')->header('style', '{url}plugins/content/TipsX3.css');
    Registry::get('page')->header('script', '{url}plugins/content/TipsX3.js');

    $cid = COM_COMMENT_CID;

    $query = Registry::get('db')->query("SELECT * FROM {prefix}comments WHERE cid = '" . $cid . "' AND component = '".COM_COMMENT_COMPONENT."' ORDER BY date ASC");
    if ((int)$query->num_rows == 0) {
        Text::br();
        Text::h2( Lang::getFront('com_comment', 'none_comments') );
    } else {
        echo "<div id=\"comments\">\n";
        while ($data = $query->fetch_array()) {
        	$author = array();
        	$name = array();
            if ((int)$data['author_id'] == 0) {
                $author['display_name'] = "<a href=\"" . strip_tags($data['web']) . "\">" . $data['username'] . "</a>";
                $name['display_name'] = $data['username'];
                $avatar = '{url}components/com_user/avatars/anonymous.png';
            } else {
                $author = User::getUserData($data['author_id']);
                $name = $author;
                $author['display_name'] = '<a href="{url}index.php?q=user/single/' . $author['id'] . '" title="AJAX:' . $GLOBALS['url'] .
                    'components/com_user/view/ajax.single.php?id=' . $data['author_id'] .
                    '" class="comment_user">' . $author['display_name'] . '</a>';
                $avatar = $author['avatar'];
                $avatar = (empty($avatar)) ? 'default.png' : $avatar;
                $avatar = '{url}components/com_user/avatars/' . $avatar;
            }
            //Smiley
            $data['text'] = Smilies::_parse_smilies($data['text']);
            //End of Smiley

            $browser = new Browser($data['useragent']);
            $browser_icon = "<img src=\"{url}libs/icons/" . strtolower($browser->
                Name) . ".png\" alt=\"" . $browser->Name . "\" />";
            $os_icon = "<img src=\"{url}libs/icons/" . strtolower($browser->
                Platform) . ".png\" alt=\"" . $browser->Platform . "\" />";
                
            $bbcode = new bbcode();
            $str = $data['text'];

            $text = $bbcode->parse($str, 'post');
                
                $tlp = Content::parseTlp('com_comment', 'singleComment.phtml');
                $tlp = str_replace('{id}', $data['id'], $tlp);
                $tlp = str_replace('{avatar}', $avatar, $tlp);
                $tlp = str_replace('{name}', $author['display_name'], $tlp);
                $tlp = str_replace('{date}', $data['date'], $tlp);
                $tlp = str_replace('{os-icon}', $os_icon, $tlp);
                $tlp = str_replace('{browser-icon}', $browser_icon, $tlp);
                $tlp = str_replace('{text}', $text, $tlp);
                
                echo $tlp;
        }
        $script = '<script type="text/javascript">
	window.addEvent(\'load\', function() {
	var theTips = new TipsX3 ($$(\'.comment_user\'), {showDelay: 0});
	});
	</script>';
        Registry::get('page')->header("text", $script);
        echo '</div>';
    }
    if (intval($_SESSION['rank']) >= Page::getPermission('comment') && !COM_COMMENT_CLOSED) {
            
    Div::startTag(null, 'comment_add');
    Text::br();
    
    echo "<script type=\"text/javascript\">edToolbar('comment_text'); </script>\n\n";
    
    $form = new Form('{url}index.php' . '?' . $_SERVER['QUERY_STRING'], 'post', true, 'fieldset', 'comment_form');
    
    $form->addGroup( Lang::getFront('com_comment', 'add_title') );
    
    if (User::isLogged())
    {
        $form->addTextbox('username', Lang::getFront('com_comment', 'user'), User::getDisplayName(), true);
    } else {
        $form->addTextbox('username', Lang::getFront('com_comment', 'user'))->notEmpty( Lang::getFront('com_comment', 'user_empty') );
        $form->addTextbox('website', Lang::getFront('com_comment', 'web'));
        $form->addTextbox('email', Lang::getFront('com_comment', 'email'));
    }
    
    $form->addTextarea('message', Lang::getFront('com_comment', 'text'), null, 10, 50, null, 'comment_text')
             ->notEmpty( Lang::getFront('com_comment', 'text_empty') );
    $form->addLine('<p id="spam"><img src="{url}libs/captcha/captcha.php" style="cursor: pointer;" title="Kontrolní kód" alt="Spam Protection" id="captcha" onclick="javascript:captcha_refresh();" /></p>', false);
    $form->addTextbox('protection', Lang::getFront('com_comment', 'captcha'), null, false, null, 'protection');
    $form->addSubmit('comment_submit', Lang::getFrontGlobal('submit'));
    
    echo $form;
    
    Div::endTag();
    
        if($form->isSubmitted() and $form->isValid()){
            if ($_SESSION['captcha'] != $_POST['protection']) {
                Text::h2( Lang::getFront('com_comment', 'captcha_bad') );
            } else {
                $username = Registry::get('db')->real_escape_string($_POST['username']);
                $website = Registry::get('db')->real_escape_string($_POST['website']);
                $email = Registry::get('db')->real_escape_string($_POST['email']);
                $bbcode = new bbcode();
                $db_str = $bbcode->parse(htmlspecialchars($_POST['message']), 'pre');

                $query = Registry::get('db')->query("INSERT INTO {prefix}comments(author_id, username, cid, component, web, email, text, date, useragent, ip, karma) VALUES('" .
                    User::getUid() . "', '" . $username . "', '" . $cid .
                    "', '".COM_COMMENT_COMPONENT."', '" . $website . "', '" . $email . "', '" . $db_str . "', NOW(), '" . $_SERVER['HTTP_USER_AGENT'] .
                    "', '" . Registry::get('visitors_online')->ipCheck() . "', '0' )");
                $log->write(8, 'com_comment', 'NOW()', 'User added comment to component '.COM_COMMENT_COMPONENT.' #' .
                    intval($cid), intval($_POST['author_id']));
                Text::h2(Lang::getFront('com_comment', 'comment_added'));
                echo '<meta http-equiv="refresh" content="2;url={url}index.php' . '?' . $_SERVER['QUERY_STRING'] . '" />';
            }
        }

    } else {
        Text::h2(Lang::getFront('com_comment', 'bad_permission') . ' <strong>' . rank2text(Page::getPermission('comment')) . '</strong> !');
    }


}
?>